var CURRENT_LINK;
$(document).ready(function(){
  $(".thumbs").each(function(){
    $(this).html('<a class="thumb-up" title="positive vote"></a> <a class="thumb-down" title="negative vote"></a><div style="clear:both"></div>')
  });
  
  $(".thumbs a").live('click',function(){
    var temp=$(this).parent().parent().parent().parent().attr('id').toString().split('-');
    var type=$(this).attr('class');
    if(type=='thumb-up'){
      vote_val=0;
      if(type=='thumb-up'){
        vote_val=1;
      }
            
      $.ajax({
        type: 'POST',
        url: DO_VOTE_URL,
        data: { value: vote_val, id: temp[1] },
        success: function(data){
          if(data.error||data.alert){
            if(data.alert) data.error=data.alert;
            $("#lnk-"+temp[1]+" .vote .votelnk").html('<span class="warn-icon">'+data.error+'</span>');
            $("#lnk-"+temp[1]+" .vote div").css('background','#f1f1f1');
            $("#lnk-"+temp[1]+" .vote div a").css('color','#ccc');
            
            if(data.alert) alert(data.alert);
          }else{
            $("#lnk-"+data.Link.id+" .vote .votelnk").html(data.Link.result);
            $("#lnk-"+data.Link.id+" .vote .votes a").html(parseInt(data.Link.votes)+parseInt(data.Link.anonymous));
            $("#lnk-"+data.Link.id+" .vote .thumbs").html(data.msg);
          }
        },
        dataType: 'json'  
      });
      
    }
    
  });
  
    
  
  $(".thumb-down").live('click',function(){
    var temp=$(this).parent().parent().parent().parent().attr('id').toString().split('-');
    CURRENT_LINK=temp[1];
    $.blockUI({ message: $('#report-link'), css: { width: '275px' } });
  });

  $(".dialog-close").click(function(){
    $.unblockUI();
    return false; 
  });
  
  $(".neg-vote").click(function(){
      
    $.ajax({
      type: 'POST',
      url: DO_VOTE_URL,
      data: { value:$(this).attr('id').toString().substring(4), id: CURRENT_LINK },
      success: function(data){
        if(data.error||data.alert){
          if(data.alert) data.error=data.alert;
          $("#lnk-"+CURRENT_LINK+" .vote .votelnk").html('<span class="warn-icon">'+data.error+'</span>');
          $("#lnk-"+CURRENT_LINK+" .vote div").css('background','#f1f1f1');
          $("#lnk-"+CURRENT_LINK+" .vote div a").css('color','#ccc');
          
          if(data.alert) alert(data.alert);
        }else{
          $("#lnk-"+CURRENT_LINK+" .vote .votelnk").html($(this).html());
          $("#lnk-"+CURRENT_LINK+" .vote div").css('background','#f1f1f1');
          $("#lnk-"+CURRENT_LINK+" .vote div a").css('color','#ccc');
        }
      }
    });
       
    $.unblockUI(); 
    return false;
  });
})